<?php
    include_once '../connection/databaseHandler.php';
    include_once '../connection/sessionHandler.php';
    $database = new DatabaseHandler();
    $session = new SessionHandler();

    $id = $_GET['id'];

    if ($_GET['ref']=="wall") {
        // menghapus pos wall
        $query = "DELETE FROM $database->t_wall WHERE id=".$id;
        $database->execQuery($query);

        // menhapus semua komentar terkait
        $query = "SELECT * FROM $database->t_comment_wall WHERE id_wall=".$id;
        $result = $database->execQuery($query);
        $count = mysql_num_rows($result);
        for ($i=0;$i<$count;$i++) {
            $query = "DELETE FROM $database->t_comment WHERE id=".mysql_result($result, $i, "id_comment");
            $database->execQuery($query);
        }
        $query = "DELETE FROM $database->t_comment_wall WHERE id_wall=".$id;
        $database->execQuery($query);

        // redirect halaman
        header("location:../profile.php?id=".$_GET['user']."&page=wall");
    }
    else if ($_GET['ref']=="message") {
        // menghapus pos wall
        $query = "DELETE FROM $database->t_message WHERE id=".$id;
        $database->execQuery($query);

        // menhapus semua komentar terkait
        $query = "SELECT * FROM $database->t_comment_message WHERE id_message=".$id;
        $result = $database->execQuery($query);
        $count = mysql_num_rows($result);
        for ($i=0;$i<$count;$i++) {
            $query = "DELETE FROM $database->t_comment WHERE id=".mysql_result($result, $i, "id_comment");
            $database->execQuery($query);
        }
        $query = "DELETE FROM $database->t_comment_message WHERE id_message=".$id;
        $database->execQuery($query);
        $query = "DELETE FROM $database->t_messagefor WHERE id_message=".$id;
        $database->execQuery($query);

        // redirect halaman
        header("location:../profile.php?page=message");
    }
    else if ($_GET['ref']=="foto") {
        $id = explode(".", $id);
        $id = $id[0];

        // menghapus foto dari hardisk
        $query = "SELECT img_src FROM $database->t_foto WHERE id=".$id;
        $result = $database->execQuery($query);
        $result = mysql_fetch_array($result);
        unlink("../".$result['img_src']);

        $query = "DELETE FROM $database->t_foto WHERE id=".$id;
        $database->execQuery($query);

        // menhapus semua komentar terkait
        $query = "SELECT * FROM $database->t_comment_foto WHERE id_foto=".$id;
        $result = $database->execQuery($query);
        $count = mysql_num_rows($result);
        for ($i=0;$i<$count;$i++) {
            $query = "DELETE FROM $database->t_comment WHERE id=".mysql_result($result, $i, "id_comment");
            $database->execQuery($query);
        }
        $query = "DELETE FROM $database->t_comment_foto WHERE id_foto=".$id;
        $database->execQuery($query);
        $query = "DELETE FROM $database->t_fotomark WHERE id_foto=".$id;
        $database->execQuery($query);

        // redirect halaman
        header("location:../profile.php?id=".$_GET['user']."&page=photo");
    }
    else if ($_GET['ref']=="comment_wall") {
        // menghapus komentar di wall
        $query = "DELETE FROM $database->t_comment WHERE id='".$id."'";
        $database->execQuery($query);
        $query = "DELETE FROM $database->t_comment_wall WHERE id_comment='".$id."'";
        $database->execQuery($query);
        // redirect halaman
        header("location:../profile.php?id=".$_GET['user']."&page=wall");
    }
    else if ($_GET['ref']=="comment_foto") {
        // menghapus komentar di foto
        $query = "DELETE FROM $database->t_comment WHERE id=".$id;
        $database->execQuery($query);
        $query = "DELETE FROM $database->t_comment_foto WHERE id_comment=".$id;
        $database->execQuery($query);

        // redirect halaman
        header("location:../profile.php?id=".$_GET['user']."&page=photo-detail&photo-id=".$_GET['pid']);
    }
?>
